package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo6Window {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val ds: DataStream[String] = env.socketTextStream("master",8888)

    val kvDS: DataStream[(String, Int)] = ds.flatMap(_.split("")).map((_,1))

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    //5秒一个窗口
    val winDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS.timeWindow(Time.seconds(5))

    //开窗之后求和
    val conutDS: DataStream[(String, Int)] = winDS.sum(1)

    conutDS.print()

    env.execute()
  }
}
